﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.IDAL;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.DAL;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.BLL
{
    public class RUser
    {
        private IUserAccess _db;
        private UserInfo _thisUser;
        private bool _isLogin;


        #region 属性区
        /// <summary>
        /// 当前用户是否管理员
        /// </summary>
        public bool IsAdmin
        {
            get { return _thisUser== null?false:_thisUser.IsAdmin; }
           
        }

        /// <summary>
        /// 当前用户是否登录
        /// </summary>
        public bool IsLogin
        {
            get { return _isLogin; }
            
        }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string Name
        {
            get { return _thisUser == null ? string.Empty : _thisUser.Name; }
            
        }

        /// <summary>
        /// 用户登录名
        /// </summary>
        public string LogName
        {
            get { return _thisUser == null ? string.Empty : _thisUser.LogName; }
            
        }

        public int ID
        {
            get { return _thisUser == null ? -1 : _thisUser.Id; }
        }

        #endregion

        public RUser()
        {
            _db = DalFactory.CreateUserAccess();
            _isLogin = false;
        }

        #region 私用函数区


        #endregion


        #region 公用函数区
        /// <summary>
        /// 登入
        /// </summary>
        /// <param name="logName">用户名</param>
        /// <param name="pwd">用户密码</param>
        /// <param name="isAdminLogin">是否系统管理员用户登录</param>
        /// <returns></returns>
        public bool Login(string logName,string pwd,bool isAdminLogin)
        {
            string md5Pwd = PublicFunction.GetMd5(pwd.ToLower());
            _thisUser = _db.GetUser(logName, md5Pwd, isAdminLogin);

            _isLogin = _thisUser != null;

            if (_isLogin)
            {
                _db.UpdateUserLoginTime(ID);
            }

            return _isLogin;

        }

        /// <summary>
        /// 登出
        /// </summary>
        public void Logout()
        {
            if (_isLogin)
            {
                _db.UpdateUserLongoutTime(ID);
            }

            _isLogin = false;
            _thisUser = null;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool ChangePwd(string oldPwd,string pwd,out string errString)
        {
            string oldPwddb;
            string oldPwdMd5;
            errString = string.Empty;

            if (!_isLogin) return false;

            if (pwd.Length < 6)
            {
                errString = "密码必须大于6位!";
                return false;
            }

            
            oldPwdMd5 = PublicFunction.GetMd5(oldPwd.ToLower());
            oldPwddb = _db.GetUserPwd(ID);
            if (oldPwd != oldPwddb)
            {
                errString = "当前密码输入错误";
                return false;
            }


            return _db.ChangePwd(ID, PublicFunction.GetMd5(pwd.ToLower()));
        }

        #endregion


    }
}
